<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :width="800" title="用户代理" @ok="handleSubmit" destroyOnClose>
    <BasicForm @register="registerForm">
      <template #storeCodeSlot="{ model, field }">
        <!-- <a-input @click="handleTypeOpen" v-model:value="model[field]" placeholder="请选择渠道" :allowClear="true"
           />
            -->
        <a-select v-model:value="model[field]" mode="tags" style="width: 100%" placeholder="请选择们门店"
          @click="handleTypeOpen" :default-active-first-option="false" :show-arrow="false" :filter-option="false"
          :open="false">
        </a-select>
      </template>

    </BasicForm>
  </BasicModal>

  <storeSelectModal @register="registerStoreSelectModal" @submit="handleStoreSubmit" ></storeSelectModal>

</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
import { formConfirmSchema } from './user.data';
import { useModal } from '/@/components/Modal';
import { useMessage } from '/@/hooks/web/useMessage';
import storeSelectModal from './storeSelectModal.vue';
import { getUserAgent, saveOrUpdateAgent, customerConfirm } from './user.api';

const [registerStoreSelectModal, { openModal: OpenStoreSelectModal }] = useModal();

  // 声明Emits
  const emit = defineEmits(['success', 'register']);
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    schemas: formConfirmSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false });
    //查询获取表单数据
    const res = await getUserAgent({ userName: data.userName });
    data = res.result ? res.result : data;
    //表单赋值
    await setFieldsValue({ ...data });
  });
  //表单提交事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      //await saveOrUpdateAgent(values);
      await customerConfirm(values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
//选择渠道
const handleTypeOpen = async () => {
  OpenStoreSelectModal(true, {
    modalTitle: "渠道选择",
  });
};

const handleStoreSubmit = (selectedStoreIds: string[]) => { // 更新绑定的模型数据 

  alert(selectedStoreIds);
  
};
</script>
